package main

import (
	"fmt"
	"sort"
)

//#link: https://leetcode-cn.com/problems/maximum-element-after-decreasing-and-rearranging/
func main() {
	arr := []int{1, 2, 3, 4, 5}
	fmt.Println(maximumElementAfterDecrementingAndRearranging(arr))
}

func maximumElementAfterDecrementingAndRearranging(arr []int) int {
	//1.排序当前数组
	sort.Ints(arr)
	//2.数组第一位为1
	arr[0] = 1
	//3.两数相差大于1时，接下来每一位变成比前一个数大1的数
	for i := 1; i < len(arr); i++ {
		if arr[i]-arr[i-1] >= 1 {
			arr[i] = arr[i-1] + 1
		}
	}
	return arr[len(arr)-1]
}
